package com.wang.experiment.experiment3.booksystem;

import javax.swing.*;
import java.awt.*;

/**
 * @author wr
 * @date 2021/10/26 15:29
 */
public class AddBookDialog extends JDialog {
    private final BookSystem bookSystem;
    private final BookManager manager;
    private JTextField nameInput;
    private JTextField authorInput;
    private JTextField publisherInput;
    private JTextField isbnInput;
    private JTextField publicationDateInput;
    private JTextField pagesInput;
    private JTextArea summaryInput;


    public AddBookDialog(BookSystem bookSystem, BookManager manager) {
        super(bookSystem, "添加书籍", true);
        this.bookSystem = bookSystem;
        this.manager = manager;
        init();
    }

    private void init() {
        Rectangle parentBounds = getParent().getBounds();
        setBounds(parentBounds.x + parentBounds.width / 4,
                parentBounds.y + parentBounds.height / 4,
                parentBounds.width / 2,
                parentBounds.height / 2);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());

        initInput();
        initButton();

        setVisible(true);
    }

    private void initInput() {
        JPanel inputPanel = new JPanel(new FlowLayout());

        JPanel namePanel = new JPanel(new FlowLayout());
        JLabel name = new JLabel("书名");
        name.setSize(30, 10);
        nameInput = new JTextField(16);
        nameInput.setSize(100, 10);
        namePanel.add(name);
        namePanel.add(nameInput);
        inputPanel.add(namePanel);

        JPanel authorPanel = new JPanel(new FlowLayout());
        JLabel author = new JLabel("作者");
        author.setSize(30, 10);
        authorInput = new JTextField(16);
        authorInput.setSize(100, 10);
        authorPanel.add(author);
        authorPanel.add(authorInput);
        inputPanel.add(authorPanel);

        JPanel publisherPanel = new JPanel(new FlowLayout());
        JLabel publisher = new JLabel("出版社");
        publisher.setSize(30, 10);
        publisherInput = new JTextField(16);
        publisherInput.setSize(100, 10);
        publisherPanel.add(publisher);
        publisherPanel.add(publisherInput);
        inputPanel.add(publisherPanel);

        JPanel isbnPanel = new JPanel(new FlowLayout());
        JLabel isbn = new JLabel("版号");
        isbn.setSize(30, 10);
        isbnInput = new JTextField(16);
        isbnInput.setSize(100, 10);
        isbnPanel.add(isbn);
        isbnPanel.add(isbnInput);
        inputPanel.add(isbnPanel);

        JPanel publicationDatePanel = new JPanel(new FlowLayout());
        JLabel publicationDate = new JLabel("出版日期");
        publicationDate.setSize(30, 10);
        publicationDateInput = new JTextField(16);
        publicationDateInput.setSize(100, 10);
        publicationDatePanel.add(publicationDate);
        publicationDatePanel.add(publicationDateInput);
        inputPanel.add(publicationDatePanel);

        JPanel pagesPanel = new JPanel(new FlowLayout());
        JLabel pages = new JLabel("页数");
        pages.setSize(30, 10);
        pagesInput = new JTextField(16);
        pagesInput.setSize(100, 10);
        pagesPanel.add(pages);
        pagesPanel.add(pagesInput);
        inputPanel.add(pagesPanel);

        JPanel summaryPanel = new JPanel(new FlowLayout());
        JLabel summary = new JLabel("摘要");
        summary.setSize(30, 10);
        summaryInput = new JTextArea(3, 16);
        summaryInput.setSize(100, 50);
        summaryPanel.add(summary);
        summaryPanel.add(summaryInput);
        inputPanel.add(summaryPanel);

        getContentPane().add(inputPanel, BorderLayout.CENTER);
    }

    private void initButton() {
        JPanel buttonPanel = new JPanel(new FlowLayout());
        JButton confirm = new JButton("确定");
        JButton cancel = new JButton("取消");
        buttonPanel.add(confirm);
        buttonPanel.add(cancel);

        confirm.addActionListener((e -> {
            if (!checkInput()) {
                JOptionPane.showMessageDialog(this, "数据填写错误", "警告", JOptionPane.WARNING_MESSAGE);
            } else {
                Book book = new Book(nameInput.getText(),
                        authorInput.getText(),
                        publisherInput.getText(),
                        isbnInput.getText(),
                        publicationDateInput.getText(),
                        Integer.parseInt(pagesInput.getText()),
                        summaryInput.getText());
                manager.addBook(book);
                //刷新显示
                bookSystem.showBookData(manager.getBookList());
                dispose();
            }
        }));
        cancel.addActionListener((e -> dispose()));

        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }

    private boolean checkInput() {
        //检查输入是否为空
        if (nameInput.getText().isEmpty() ||
                authorInput.getText().isEmpty() ||
                publisherInput.getText().isEmpty() ||
                isbnInput.getText().isEmpty() ||
                publicationDateInput.getText().isEmpty() ||
                pagesInput.getText().isEmpty()) {
            //摘要可以为空
            return false;
        }

        //检查页数输入是否为数字
        try {
            Integer.parseInt(pagesInput.getText());
        } catch (NumberFormatException e) {
            return false;
        }
        //日期什么的乱输我也没办法啊qwq
        return true;
    }
}
